package nl.ceasaro.jeslee.core.dao;

import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import nl.ceasaro.jeslee.core.exception.NotFoundException;

import java.io.Serializable;
import java.util.List;

public interface GenericDAO<T, ID extends Serializable>
{
   /**
    * Returns the number of entities in this container
    *
    * @return the number of entities in this container
    */
   int size();

   /**
    * Get all entities in this container
    *
    * @return List of all entities
    */
   List<T> findAll();

   /**
    * Try and find an entity with specified id
    *
    * @param id - Id the wanted entity
    * @return entity instance or null if not found
    */
   T findById(ID id);

   /**
    * Get an entity with specified ud
    *
    * @param id the id of the returned instance
    * @return entity instance
    * @throws NotFoundException when no entity exists in this container
    */
   T get(ID id) throws NotFoundException;

   /**
    * Add a new entity to this container
    *
    * @param instance the instance to save
    * @return the unique identifier for the entity in this container
    */
   ID save(T instance);

   /**
    * Remove an entity from this container,
    * does nothing if entity does not exist in this container
    *
    * @param instance the instance to delete
    */
   void delete(T instance);

   @SuppressWarnings("unchecked")
   @Transactional(propagation = Propagation.REQUIRED)
   void update(T instance);
}
